

let g4 = [
    {x:0,y:0},
    {x:0,y:2},
    {x:2,y:0},
    {x:2,y:2},
];

let g6 = [
    {x:0,y:0},
    {x:0,y:3},
    {x:2,y:0},
    {x:2,y:3},
    {x:4,y:0},
    {x:4,y:3},
];

let g9 = [
    {x:0,y:0},
    {x:0,y:3},
    {x:0,y:6},
    {x:3,y:0},
    {x:3,y:3},
    {x:3,y:6},
    {x:6,y:0},
    {x:6,y:3},
    {x:6,y:6},
];

class BoardInfo{
    constructor(board){
        this.board = board;
        this.boardSize = board.length;

        let gongCount = this.gongCount = board.length; // 宫数
        this.gongRowCount = Math.floor(Math.sqrt(gongCount)); // 宫包含几行
        this.gongColCount = Math.floor(gongCount / this.gongRowCount); // 宫包含几列

        console.log(`gongCount:${gongCount}`);
        console.log(`this.gongRowCount:${this.gongRowCount}`);
        console.log(`this.gongColCount:${this.gongColCount}`);
    }


    calGongIdx(i,j){
        let x = Math.floor(i / this.gongRowCount);
        let y = Math.floor( j / this.gongColCount);
        let countPerRow = this.boardSize / this.gongColCount;
        return countPerRow * x + y;
    }

    getGongStartIdx(gongIdx){
        switch (this.boardSize) {
            case 4:
                return g4[gongIdx];
            case 6:
                return g6[gongIdx];
            case 9:
                return g9[gongIdx];
            default:
                throw new Error(`error size:${size}`);
        }
    }
}

// let initBoard = [
//     [0,1,4,3],
//     [4,3,1,2],
//     [0,4,2,0],
//     [0,2,3,0],
// ];


let bTest = false;
if(bTest){
    let initBoard = [
        [5,6,3,1,0,4],
        [2,0,0,0,5,6],
        [3,2,0,4,0,1],
        [0,1,6,0,3,0],
        [1,5,0,6,0,3],
        [6,0,4,0,1,2],
    ];

    let info = new BoardInfo(initBoard);


    for(let i = 0; i < 6;++i){
        for(let j = 0; j < 6; ++j){
            console.log(`i:${i},j:${j}->${info.calGongIdx(i,j)}`);
        }
    }
}

module.exports = BoardInfo;
